package com.xxx.common.shardingsphere.config;

import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;

import java.util.Collection;
import java.util.stream.Collectors;


public class ForceTableHintAlgorithm implements HintShardingAlgorithm<Integer> {

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<Integer> shardingValue) {

        String tableName = shardingValue.getLogicTableName();

        // 过滤出真实存在的表
        return shardingValue.getValues().stream()
                .map(tableSuffix -> tableName + "_" + tableSuffix)
                .filter(availableTargetNames::contains)  // 过滤出真实存在的表
                .collect(Collectors.toList());
    }

}